<?php
/*
 * IKPHP 爱客开源社区 @copyright (c) 2012-3000 IKPHP All Rights Reserved 
 * @Author 小麦
 * @Email:ikphp@sina.cn
 * @Description:网站基础前端控制器类
 */
class controller_frontend extends controller_base {
	protected $visitor = null;
	protected function _init() {
		parent::_init ();
		//初始化访问者
		$this->_init_visitor ();
	
		//初始化网站前端
		$this->_initWebSite();

	}
	/**
	 * 初始化访问者
	 */
	private function _init_visitor() {
		if (is_login ()) {
			$this->visitor = $user_auth = session ( "user_auth" );
			$count_msg_unread = D ( 'common/message' )->getUnreadNum ( $this->visitor ['userid'] );
			$count_new_msg = $count_msg_unread > 0 ? $count_msg_unread : 0;
			$this->assign ( 'count_new_msg', $count_new_msg );
		}
		$this->assign ( 'visitor', $this->visitor );
	}
	/**
	 * SEO设置
	 */
	private function _initWebSite() {
		$this->assign('topNav',$this->_topnav());
	}
	/**
	 * SEO设置
	 */
	protected function _seo($seo_info = array(), $data = array()) {
		$page_seo = array ('title' => C ( 'ik_site_title' ), 'subtitle' => C ( 'ik_site_subtitle' ), 'keywords' => C ( 'ik_site_keywords' ), 'description' => C ( 'ik_site_desc' ) );
		$page_seo = array_merge ( $page_seo, $seo_info );
		//开始替换
		$searchs = array ('{site_name}', '{site_title}', '{site_keywords}', '{site_desc' );
		$replaces = array (C ( 'ik_site_title' ), C ( 'ik_site_subtitle' ), C ( 'ik_site_keywords' ), C ( 'ik_site_desc' ) );
		preg_match_all ( "/\{([a-z0-9_-]+?)\}/", implode ( ' ', array_values ( $page_seo ) ), $pageparams );
		if ($pageparams) {
			foreach ( $pageparams [1] as $var ) {
				$searchs [] = '{' . $var . '}';
				$replaces [] = $data [$var] ? strip_tags ( $data [$var] ) : '';
			}
			//符号
			$searchspace = array ('((\s*\-\s*)+)', '((\s*\,\s*)+)', '((\s*\|\s*)+)', '((\s*\t\s*)+)', '((\s*_\s*)+)' );
			$replacespace = array ('-', ',', '|', ' ', '_' );
			foreach ( $page_seo as $key => $val ) {
				$page_seo [$key] = trim ( preg_replace ( $searchspace, $replacespace, str_replace ( $searchs, $replaces, $val ) ), ' ,-|_' );
			}
		}
		$this->assign ( 'seo', $page_seo );
	}
	// 顶部次导航
	private function _topnav() {
		$arrNav = $arrApp = array ();
		$arrNav ['index'] = array ('name' => '首页', 'url' => C ( 'ik_site_url' ) );
		$arrApp = S('site_apps');
	    if (false === $arrApp) {
            $arrApp = D('common/app')->app_cache();
        }	
		foreach ( $arrApp as $item ) {
			if (empty ( $item ['app_entry'] )) {
				$item ['app_entry'] = 'index/index';
			}
			$arrNav [$item ['app_name']] = array ('name' => $item ['app_alias'], 'url' => U ( $item ['app_name'] . '/' . $item ['app_entry'] ) );
		}
		return $arrNav;
	}
}